package is.ru.honn.rumblr.service;

import is.ru.honn.rumblr.domain.AbstractRumblrPost;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class DefaultStatisticStrategy implements StatisticsStrategy
{
  Map<String, Summary> counters = new HashMap<String, Summary>();
  Summary total = new Summary("Total", 0,0);

  public void count(AbstractRumblrPost post)
  {
    Summary c = counters.get(post.getType());
    int t = post.getTags().size();
    if(null == c)
    {
      counters.put(post.getType(), new Summary(post.getType(), 1, t));
    }
    else
    {
      counters.put(post.getType(), new Summary(post.getType(), ++c.total, c.totalTags + t));
    }
    total.total++;
    total.totalTags += t;
  }

  public String getSummary()
  {
    StringBuffer sb = new StringBuffer();
    sb.append("\n\nTotal posts: " + total.total + " tags: " + total.totalTags + "\n");

    Collection col = counters.values();
    Summary c;
    for(Object o : col)
    {
      c = (Summary)o;
      sb.append(c.type + ":" + c.total + " tags: " + c.totalTags + "\n");
    }

    return sb.toString();
  }
}

class Summary
{
  String type;
  int total;
  int totalTags;

  Summary(String type, int total, int totalTags)
  {
    this.type = type;
    this.total = total;
    this.totalTags = totalTags;
  }
}
